import 'dart:math';
import 'package:flutter/material.dart';

import 'number_progress.dart';
/**
 * author：lx
 * date：2021-0126
 * descr：更新内容、进度界面
 */
class UpdateWidget extends StatelessWidget {
  /// 对话框的宽度
  final double width;

  /// 升级标题
  final String? title;

  /// 更新内容
  final String? updateContent;

  /// 标题文字的大小
  final double titleTextSize;

  /// 更新文字内容的大小
  final double contentTextSize;

  /// 按钮文字的大小
  final double buttonTextSize;

  /// 顶部图片
  final Widget? topImage;
  final Widget? closeImage;

  /// 拓展高度(适配顶部图片高度不一致的情况）
  final double extraHeight;

  /// 边框圆角大小
  final double radius;

  /// 主题颜色
  final Color themeColor;

  /// 更新事件
  final VoidCallback? onUpdate;

  /// 可忽略更新
  final bool enableIgnore;

  /// 更新事件
  final VoidCallback? onIgnore;

  double progress;

  /// 进度条的背景颜色
  final Color progressBackgroundColor;

  /// 更新事件
  final VoidCallback? onClose;

  /// 是否是强制更新
  final bool isForce;

  /// 更新按钮内容
  final String updateButtonText;

  /// 忽略按钮内容
  final String ignoreButtonText;

  UpdateWidget(
      {Key? key,
        this.width = 0.0,
        @required this.title,
        @required this.updateContent,
        @required this.onUpdate,
        this.titleTextSize = 16.0,
        this.contentTextSize = 14.0,
        this.buttonTextSize = 14.0,
        this.progress = -1.0,
        this.progressBackgroundColor = const Color(0xFFFFCDD2),
        this.topImage,
        this.closeImage,
        this.extraHeight = 5.0,
        this.radius = 4.0,
        this.themeColor = Colors.red,
        this.enableIgnore = false,
        this.onIgnore,
        this.isForce = false,
        this.updateButtonText = '更新',
        this.ignoreButtonText = '忽略此版本',
        this.onClose
      })
      : super(key: key);
  @override
  Widget build(BuildContext context) {
    double dialogWidth =
    width <= 0 ? getFitWidth(context) * 0.618 : width;
    return Material(
        type: MaterialType.transparency,
        child: Container(
          child: SizedBox(
            width: dialogWidth,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                SizedBox(
                  width: dialogWidth,
                  child: topImage != null
                      ? topImage
                      : Image.asset(
                      'assets/images/update_bg_app_top.png',
                    fit: BoxFit.fill,
                    package: 'tp_upgrade_lib',
                  ),
                ),
                Container(
                  width: dialogWidth,
                  alignment: Alignment.center,
                  padding:
                  EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 8),
                  decoration: ShapeDecoration(
                    color: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(radius),
                          bottomRight: Radius.circular(radius)),
                    ),
                  ),
                  child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Container(
                            padding: EdgeInsets.only(top: extraHeight),
                            child: Text(title??'',
                                style: TextStyle(
                                    fontSize: titleTextSize,
                                    color: Colors.black)),
                          ),
                          Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Text(updateContent??'',
                                style: TextStyle(
                                    fontSize: contentTextSize,
                                    color: Color(0xFF666666))),
                          ),
                          progress < 0
                              ? Column(children: <Widget>[
                            FractionallySizedBox(
                              widthFactor: 1,
                              child: RaisedButton(
                                shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(5)),
                                elevation: 0,
                                highlightElevation: 0,
                                child: Text(updateButtonText,
                                    style: TextStyle(
                                        fontSize: buttonTextSize)),
                                color: themeColor,
                                textColor: Colors.white,
                                onPressed: onUpdate,
                              ),
                            ),
                            enableIgnore && onIgnore != null
                                ? FractionallySizedBox(
                              widthFactor: 1,
                              child: FlatButton(
                                shape: RoundedRectangleBorder(
                                    borderRadius:
                                    BorderRadius.circular(5)),
                                child: Text(ignoreButtonText,
                                    style: TextStyle(
                                        fontSize:
                                        buttonTextSize)),
                                materialTapTargetSize:
                                MaterialTapTargetSize.shrinkWrap,
                                textColor: Color(0xFF666666),
                                onPressed: onIgnore,
                              ),
                            )
                                : SizedBox()
                          ])
                              : NumberProgress(
                              value: progress,
                              backgroundColor: progressBackgroundColor,
                              valueColor: themeColor,
                              padding: EdgeInsets.symmetric(vertical: 10))
                        ],
                      )),
                ),
                !isForce
                    ? Column(children: <Widget>[
                  SizedBox(
                      width: 1.5,
                      height: 50,
                      child: DecoratedBox(
                          decoration:
                          BoxDecoration(color: Colors.white))),
                  IconButton(
                    iconSize: 30,
                    constraints:
                    BoxConstraints(maxHeight: 30, maxWidth: 30),
                    padding: EdgeInsets.zero,
                    icon: closeImage??Image.asset(
                      'assets/images/update_ic_close.png',
                      package: 'tp_upgrade_lib',
                    ),
                    onPressed: onClose,
                  )
                ])
                    : SizedBox()
              ],
            ),
          ),
        ));
  }

  double getFitWidth(BuildContext context) {
    return min(getScreenHeight(context), getScreenWidth(context));
  }

  double getScreenHeight(BuildContext context) {
    return MediaQuery.of(context).size.height;
  }

  double getScreenWidth(BuildContext context) {
    return MediaQuery.of(context).size.width;
  }
}